package com.example.smartg;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.smartg.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;
import tp.xmaihh.serialport.bean.ComBean;
import tp.xmaihh.serialport.utils.ByteUtil;

public class LvAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater layoutInflater;
    public String ge;
    private List<Order> records;
    private Order order;

    public LvAdapter(Context context, String ge, List<Order> records) {
        this.context = context;
        this.ge = ge;
        layoutInflater = LayoutInflater.from(context);
        this.records = records;
    }

    @Override
    public int getCount() {
        return this.records.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    static class ViewHolder {
        public TextView batch_no;
        public Button delete_btn;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.lv_item, null);
            viewHolder = new ViewHolder();
            viewHolder.batch_no = (TextView) view.findViewById(R.id.batch_no);
            viewHolder.delete_btn = (Button) view.findViewById(R.id.delete_btn);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
//        viewHolder.cb_item.setChecked(this.toDeletes.get(i));
        viewHolder.batch_no.setText(this.records.get(i).batch_num);
        LvAdapter self = this;
        viewHolder.delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteItem(records.get(i).batch_num, "YS001", i);
            }
        });
        return view;
    }

    public void deleteItem(String order_num, String ge, int index) {
        String url = "http://10.2.47.115/smartg_api/deposit_manage/delete_material";
        //实例化
        com.example.smartg.HttpUtil httpUtil = new HttpUtil();
        JSONObject json = new JSONObject();
        try {
            json.put("batch_no", order_num);
            json.put("cabinet_id", ge);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        httpUtil.sendOkHttpRequest(url, json.toString(), new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("exception", e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responsedata = response.body().string();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(responsedata);
                    String msg = jsonObject.optString("msg");
                    int code = jsonObject.optInt("code");
                    if (code == 20000) {
                        Message message = Message.obtain();
                        message.what = index;
                        mHandler.sendMessage(message);
                    } else {
                        Message message = Message.obtain();
                        message.what = 9999;
                        mHandler.sendMessage(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 9999) {
                ToastUtil.success(context, "删除失败!");
                return false;
            } else {
                records.remove(msg.what);
                notifyDataSetChanged();
                ToastUtil.success(context, "删除成功!");
                if (records.size() == 0) {
                    ge = "";
                }
                return true;
            }

        }
    });

}
